from fastapi import FastAPI
from pydantic import BaseModel
import httpx
import re

app = FastAPI(title="MCP智能Agent")

# 服务端口映射
def get_service_url(service: str):
    base = "http://127.0.0.1:"
    ports = {
        "weather": "8001",
        "hotel": "8002",
        "tour": "8003",
        "quote": "8004"
    }
    return base + ports[service]

class AgentRequest(BaseModel):
    user_input: str

# 提取城市名的简单方法（可扩展）
def extract_city(text):
    cities = ["北京", "上海", "广州", "深圳", "成都", "杭州"]
    for city in cities:
        if city in text:
            return city
    return None

@app.post("/agent")
def agent_router(req: AgentRequest):
    user_input = req.user_input.strip()
    # 天气服务
    if re.search(r"天气|气温|下雨|晴|阴", user_input):
        if "一周" in user_input or "未来" in user_input:
            city = extract_city(user_input)
            if not city:
                return {"error": "未识别到城市名"}
            url = f"{get_service_url('weather')}/weather/week?city={city}"
            r = httpx.get(url, timeout=5)
            return r.json()
        else:
            city = extract_city(user_input)
            if not city:
                return {"error": "未识别到城市名"}
            url = f"{get_service_url('weather')}/weather/today?city={city}"
            r = httpx.get(url, timeout=5)
            return r.json()
    # 酒店服务
    if re.search(r"酒店|宾馆|房间|预定|订房", user_input):
        if "预定" in user_input or "订" in user_input:
            city = extract_city(user_input)
            if not city:
                return {"error": "未识别到城市名"}
            hotel = None
            # 简单从输入中提取酒店名
            hotels = ["国际大酒店", "丽都酒店", "浦东大酒店", "外滩酒店", "白云宾馆", "花园酒店", "福田酒店", "罗湖酒店", "锦江宾馆", "环球中心酒店", "西湖大酒店", "滨江酒店"]
            for h in hotels:
                if h in user_input:
                    hotel = h
                    break
            if not hotel:
                return {"error": "未识别到酒店名"}
            url = f"{get_service_url('hotel')}/reserve?city={city}&hotel={city}{hotel}"
            r = httpx.post(url, timeout=5)
            return r.json()
        else:
            city = extract_city(user_input)
            if not city:
                return {"error": "未识别到城市名"}
            url = f"{get_service_url('hotel')}/hotels?city={city}"
            r = httpx.get(url, timeout=5)
            return r.json()
    # 旅游服务
    if re.search(r"旅游|景点|游玩|推荐", user_input):
        city = extract_city(user_input)
        if not city:
            return {"error": "未识别到城市名"}
        url = f"{get_service_url('tour')}/tourspots?city={city}"
        r = httpx.get(url, timeout=5)
        return r.json()
    # 支持的城市列表
    if re.search(r"支持的城市|城市列表|有哪些城市", user_input):
        # 默认返回天气服务支持城市
        url = f"{get_service_url('weather')}/cities"
        r = httpx.get(url, timeout=5)
        return {"weather_cities": r.json()}
    # 每日语录
    if re.search(r"语录|一句|经典|名言|鸡汤", user_input):
        url = f"{get_service_url('quote')}/quote"
        r = httpx.get(url, timeout=5)
        return r.json()
    return {"error": "暂不支持该请求，请换个表达方式。"}
